package renguanyu.app.auth4fx.util;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.sqlite.SQLiteDataSource;

import java.sql.SQLException;

public class DbUtil {

    private static final SQLiteDataSource dataSource;
    private static final QueryRunner queryRunner;

    static {
        dataSource = new SQLiteDataSource();
        dataSource.setUrl("jdbc:sqlite:auth4fx.db");
        queryRunner = new QueryRunner(dataSource);
    }

    public static <T> T query(String sql, ResultSetHandler<T> rsh) {
        try {
            return queryRunner.query(sql, rsh);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> T query(String sql, ResultSetHandler<T> rsh, Object... params) {
        try {
            return queryRunner.query(sql, rsh, params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static int update(String sql, Object... params){
        try {
            return queryRunner.update(sql,params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static int[] batch(String sql, Object[][] params){
        try {
            return queryRunner.batch(sql,params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}
